jQuery( 'document' ).ready( function( $ ){
    (function() {    
        $.ivycat_ajax_do = function( ajaxData, callback ){
            return $.post( ICSaconn.ajaxurl, ajaxData, callback );       
        }
       
        if( $( '#ivycat-slider' ).length ){
            var slide_start = 1;
             $.ivycat_ajax_do( { 'action' : 'get-slides', 'slider-dets' : $( '#slider-dets' ).val() }, function( resp ){
                slides = $.parseJSON( resp );
				//console.log( slides );
                rotateSwitch(  );
            });
             
            function advance_slideshow(){
                //console.log( posts );
                var total = slide_length();
                
                if( total < 2 ) return;
                var current = slide_start;
                
                var next = current+1;
                if( total == next ){
                    slide_start = 0;
                    next = 0;
                }else{
                    slide_start = current +1;
                }
                
                $( '#ivycat-slider .screen' ).customFadeOut( 1000, function(){
                    if( slides[current].slide_image.length > 1 ){
						if( $( '#ivycat-slider .screen img' ).length < 1 ){
							$( '#ivycat-slider .screen').prepend( '<img src=""/>' );
						}
						$( '#ivycat-slider .screen img' ).attr( 'src', slides[current].slide_image  );
						$( '#ivycat-slider .screen img' ).attr( 'width', slides[current].slide_image_width  );
						$( '#ivycat-slider .screen img' ).attr( 'height', slides[current].slide_image_height  );
					}else{
						$( '#ivycat-slider .screen img' ).remove();
					}
                    $( '#ivycat-slider h3' ).html( slides[current].slide_title  );
                    $( '#ivycat-slider div.slider-content' ).html( slides[current].slide_content  );
                    //$( '#ivycat-slider a' ).attr( 'href', slides[current].slide_link  );
                    $( '#ivycat-slider .screen' ).customFadeIn( 1000, function(){});
                });
                
            }
            
            rotateSwitch = function( ){
                play = setInterval(function( ){ //Set timer - this will repeat itself every 8 seconds
                    if( slides ) advance_slideshow();
                }, 8000); //Timer speed in milliseconds (8 seconds)
            };
            
        }
        
        function slide_length(){
                var count = 0;
                $.each( slides, function(){ count +=1; });
                return count;
            };
        
        //alert($.browser.version);
        $.fn.customFadeIn = function(speed, callback) {
            $(this).fadeIn(speed, function() {
                if(jQuery.browser.msie)
                    $(this).get(0).style.removeAttribute('filter');
                if(callback != undefined)
                    callback();
            });
        };
        $.fn.customFadeOut = function(speed, callback) {
            $(this).fadeOut(speed, function() {
                if(jQuery.browser.msie)
                    $(this).get(0).style.removeAttribute('filter');
                if(callback != undefined)
                    callback();
            });
        };
    })()
});